<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    
    
    <title>对象和包装类 | Liuqh&#39;blogs | 要有最朴素的生活  最遥远的梦想，即使明日天寒地冻 路远马亡。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="keywords" content="对象,包装类">
    <link rel="shortcut icon" href="/img/favicon.ico">
    <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.2/dist/jquery.fancybox.min.css">
    <link rel="stylesheet" href="/css/style.css?v=1.4.3">
    
    <script type="text/javascript">
        // Data Center
        var DC = {
            reward:	true,
            lv: JSON.parse('{"enable":false,"app_id":null,"app_key":null,"icon":true}'),
            v: JSON.parse('{"enable":false,"appid":null,"appkey":null,"notify":true,"verify":true,"placeholder":"give me some sugers plz...","avatar":"wavatar"}'),
            g: JSON.parse('{"enable":false,"lazy":false,"owner":"liuqinghua0609","repo":"gitment","oauth":{"client_id":null,"client_secret":null},"perPage":10}'),
            d: JSON.parse('{"app_id":null}')
        };
    </script>
    <script type="text/javascript">
        window.lazyScripts=[];
    </script>
    
</head>


<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="">
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      
      <img src="/img/111.jpg" class="brand-bg">
      
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Liuqh</h5>
          <a href="mailto:心随远方 无畏无惧" title="心随远方 无畏无惧" class="mail">
            
              <span>心</span>
            
              <span>随</span>
            
              <span>远</span>
            
              <span>方</span>
            
              <span> </span>
            
              <span>无</span>
            
              <span>畏</span>
            
              <span>无</span>
            
              <span>惧</span>
            
          </a>
        </hgroup>
        
        <ul class="menu-link">
          
              <li>
                <a href="https://blog.csdn.net/qq_41840644" target="_blank">
                  <i class="icon icon-lg icon-heart"></i>
                </a>
              </li>
            
              <li>
                <a href="https://weibo.com/5902408774/profile?rightmod=1&amp;wvr=6&amp;mod=personnumber&amp;is_all=1" target="_blank">
                  <i class="icon icon-lg icon-weibo"></i>
                </a>
              </li>
            
        </ul>
        
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                首页
              </a>
            </li>
        
            <li class="">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="">
              <a href="/custom"  >
                <i class="icon icon-lg icon-music"></i>
                随身听
              </a>
            </li>
        
            <li class="">
              <a href="/about"  >
                <i class="icon icon-lg icon-user"></i>
                我
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row clearfix">
        <a href="javascript:;" class="header-icon pull-left waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">
            <span>对象和包装类</span>
            
        </div>
        
        <a href="javascript:;" id="site_search_btn" class="header-icon pull-right waves-effect waves-circle waves-light">
            <i class="icon icon-lg icon-search"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">
    <img src="/img/banner1.jpg" class="header-bg">
    <div class="container fade-scale">
        <h1 class="title">对象和包装类</h1>
        <h5 class="subtitle">
            
                <time datetime="2019-07-17T15:35:48.000Z" itemprop="datePublished" class="page-time">
  2019-07-17
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/JavaScript学习笔记/">JavaScript学习笔记</a></li></ul>

            
        </h5>
        
    </div>
    

</header>

<div id="site_search">
    <div class="search-title clearfix">
        <span class="pull-left">
          <i class="icon icon-lg icon-search"></i>
        </span>
        <input type="text" id="local-search-input" name="q" results="0" placeholder="search my blog..." class="form-control pull-left"/>
        <a href="javascript:;" class="close pull-right waves-effect waves-circle waves-light">
          <i class="icon icon-lg icon-close"></i>
        </a>
    </div>
    <div id="local-search-result"></div>
</div>


<div class="container body-wrap">
    <article id="post-对象和包装类"
  class="post-article article-type-post" itemprop="blogPost">
    <div class="post-card">
        <h1 class="post-card-title">对象和包装类</h1>
        <div class="post-meta">
            <time class="post-time" title="2019-07-17 23:35:48" datetime="2019-07-17T15:35:48.000Z"  itemprop="datePublished">2019-07-17</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/JavaScript学习笔记/">JavaScript学习笔记</a></li></ul>



            

            


            
        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            
            <figure class="image-box">
                <a rel=对象和包装类 href="/img/pic/1.jpg" title="这是一只猫" data-fancybox="images"><img src="/img/pic/1.jpg" alt="这是一只猫" title class></a>
                <p>这是一只猫</p>
            </figure>
<h3 id="一、对象"><a href="#一、对象" class="headerlink" title="一、对象"></a>一、对象</h3><blockquote>
<h4 id="对象的定义："><a href="#对象的定义：" class="headerlink" title="对象的定义："></a>对象的定义：</h4><ul>
<li>字面量<br><code>var obj = { }</code></li>
<li>通过构造函数：<ul>
<li>系统自带的：new Object(); Array() ; Number() ; Boolean()</li>
<li>自定义<br><code>function Person(){}</code><br><code>var person = new Person();</code><br>需要注意的是自定义构造函数的命名规则需要遵守大驼峰规则，即所有单词的首字母都需要大写。</li>
</ul>
</li>
<li>Object.create( 原型 )方法<br><code>var person = Object.create(Object.prototype)</code><br>需要注意的是这个方法里的参数可以填写null,这时这个对象是没有原型的，所以他最终的执行也不是object.prototype,但是这个参数是必须要填的，不能为空<br><code>var person = Object.prototype(null)</code><br>控制台打印出来的该person对象：<figure class="image-box">
                <a rel=对象和包装类 href="/img/pic/1563233312772.png" title="控制台图片" data-fancybox="images"><img src="/img/pic/1563233312772.png" alt="控制台图片" title class></a>
                <p>控制台图片</p>
            </figure>
</li>
</ul>
</blockquote>
<blockquote>
<h4 id="自定义构造函数内部原理："><a href="#自定义构造函数内部原理：" class="headerlink" title="自定义构造函数内部原理："></a>自定义构造函数内部原理：</h4><p>构造函数能够构造出来对象的前提是需要 new，那么在new的时候，构造函数内部会发生隐式三步<br> 1 .  在函数体最前面隐式的加上 this = { }；<br> 2 .  执行this.xxx = xxx;<br> 3 .  函数末尾隐式的 return this；</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">function Person()&#123;</span><br><span class="line"> //当new Person（）时就会发生隐式的三步</span><br><span class="line"> // var this =&#123; &#125; 当然这个this是没这简单，他对象的内部很复杂，不过我们现阶段可以这样理解</span><br><span class="line"> this.name = &apos;1&apos;;</span><br><span class="line"> this.sayHi = function()&#123;</span><br><span class="line"> console.log(&apos;hi&apos;);</span><br><span class="line"> &#125;</span><br><span class="line"> //return this;</span><br><span class="line">&#125;</span><br><span class="line">new Person();</span><br></pre></td></tr></table></figure>

<hr>
### 二、包装类
> + String( )
> + Boolean ( )
> + Number( )
> 我们知道原始值是没有方法可以调用的，只有对象类型才可以调用方法。
> 但是我们又经常会看到一种现象
> ` var str = 'abcd'`
> ` console.log(str.length) //4`
> str明明是字符串形式的原始值，但是为什么它可以调用length方法呢，
> 其实当str点方法的时候它就不是原始类型的字符串了，而是String类型的，它在内部进行隐式的转换
> <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt; <span class="keyword">var</span> str = <span class="string">'abcd'</span>;</span><br><span class="line">&gt; <span class="comment">// new String(str).length ; </span></span><br><span class="line">&gt; <span class="built_in">console</span>.log(str.length);</span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>

<blockquote>
<hr>
这就叫做 **包装类**，但是我们需要注意的一点是，当发生隐式装换的时候，他转换完成会及时的删除这个属性
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;<span class="keyword">var</span> str = <span class="string">'abcd'</span>;</span><br><span class="line">&gt;str.name = <span class="string">'hahah'</span>; <span class="comment">// new String(str).name = 'hahah'; delate</span></span><br><span class="line">&gt;<span class="built_in">console</span>.log(str.name);<span class="comment">// undefined,当他访问这个属性的时候他依然会调用new String(str).name 但是我们需要注意，上面哪行隐式包装，在语句执行完毕后已经被删除了，所以在这里是访问不到的，因此打印的是undefined。</span></span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>接下来我们看一道题</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;<span class="keyword">var</span> str = <span class="string">'1234'</span>;</span><br><span class="line">&gt;str.length = <span class="number">2</span>;</span><br><span class="line">&gt;<span class="built_in">console</span>.log(str); <span class="comment">// '1234'</span></span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<h3 id="一定不要忘记包装类之后它会delate。"><a href="#一定不要忘记包装类之后它会delate。" class="headerlink" title="一定不要忘记包装类之后它会delate。"></a>一定不要忘记包装类之后它会delate。</h3></blockquote>

        </div>
        
<blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    Last updated: <time datetime="2019-07-26T12:00:36.723Z" itemprop="dateUpdated">2019-07-26 20:00:36</time>
</span><br>


        
        转载注明出处，原文地址：<a href="/2019/07/17/对象和包装类/" target="_blank" rel="external">http://liuqinghua0609.gitee.io/2019/07/17/对象和包装类/</a>
        
    </div>
    <footer>
        <a href="http://liuqinghua0609.gitee.io">
            <img src="/img/avatar.jpg" alt="Liuqh">
            Liuqh
        </a>
    </footer>
</blockquote>

        
            <div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>

            
        
        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/包装类/">包装类</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/对象/">对象</a></li></ul>

            <div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://liuqinghua0609.gitee.io/2019/07/17/对象和包装类/&title=《对象和包装类》 — Liuqh'blogs&pic=http://liuqinghua0609.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://liuqinghua0609.gitee.io/2019/07/17/对象和包装类/&title=《对象和包装类》 — Liuqh'blogs&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>

        </div>
        
            


        
    </div>
    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="prev">
      <a href="/2019/07/20/记录今天走进的思维误区/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">this指向问题</h4>
      </a>
    </div>
  

  
    <div class="next">
      <a href="/2019/07/17/数组与类数组/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">数组与类数组</h4>
      </a>
    </div>
  
</nav>


    
    
        <aside class="post-widget">
            <nav class="post-toc-wrap" id="post-toc">
                <strong>目录</strong>
                <ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#一、对象"><span class="post-toc-number">1.</span> <span class="post-toc-text">一、对象</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#对象的定义："><span class="post-toc-number">1.1.</span> <span class="post-toc-text">对象的定义：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#自定义构造函数内部原理："><span class="post-toc-number">1.2.</span> <span class="post-toc-text">自定义构造函数内部原理：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#一定不要忘记包装类之后它会delate。"><span class="post-toc-number">2.</span> <span class="post-toc-text">一定不要忘记包装类之后它会delate。</span></a></li></ol>
            </nav>
            <div class="toc-bar"><div>
        </aside>
    
</article>

    <div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        我们一起来让这个世界有趣一点
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/reward-wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/reward-wechat.jpg" data-alipay="/img/reward-alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>

    

</div>

    </main>
    <footer class="footer ">
    
    <div class="bottom">
        <p>
            <span>
                Liuqh &copy; 2017 - 2019
            </span>
        		
           	
            
            
            <span>
	            Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/codefine/hexo-theme-mellow" target="_blank">mellow</a>
            </span>
            
            
            

            
                
<span class="site-uv" title="总访客量">
    <i class="icon icon-user"></i>
    <i class="busuanzi-value" id="busuanzi_value_site_uv"></i>
</span>


<span class="site-pv" title="总访问量">
    <i class="icon icon-eye"></i>
    <i class="busuanzi-value" id="busuanzi_value_site_pv"></i>
</span>

            
        </p>
    </div>
</footer>

    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://liuqinghua0609.gitee.io/2019/07/17/对象和包装类/&title=《对象和包装类》 — Liuqh'blogs&pic=http://liuqinghua0609.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://liuqinghua0609.gitee.io/2019/07/17/对象和包装类/&title=《对象和包装类》 — Liuqh'blogs&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>


    
    <!-- main-js -->
<script type="text/javascript" src="//cdn.bootcss.com/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="/js/plugins/fastclick.js?v=1.4.3"></script>
<script type="text/javascript" src="/js/plugins/ios-orientationchange-fix.js?v=1.4.3"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.2/dist/jquery.fancybox.min.js"></script>

<script type="text/javascript" src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>

<script type="text/javascript" src="/js/method.js?v=1.4.3"></script>
<script type="text/javascript" src="/js/blog.js?v=1.4.3"></script>

<!-- third-party -->






<script type="text/javascript" src="/js/plugins/local_search.js?v=1.4.3"></script>
<script type="text/javascript">
	var search_path = "search.xml";
	if (search_path.length === 0) {
		search_path = "search.xml";
	}
	var path = "/" + search_path;
	searchFunc(path, "local-search-input", "local-search-result");
</script>



<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



    
    





    <!-- mathjax config similar to math.stackexchange -->
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
            processEscapes: true,
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>







    
</body>
</html>
